package org.dromara.data.dnnyun.controller;

import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.data.dnnyun.service.IDnnyunDataService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.curd.dnnyun.domain.vo.DnnyunStoreVo;
import org.dromara.curd.dnnyun.domain.bo.DnnyunStoreBo;
import org.dromara.curd.dnnyun.service.IDnnyunStoreService;

/**
 * 东云区域门店
 *
 * @author wdm
 * @date 2024-02-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/data/dnnyun/store")
public class DnnyunStoreController extends BaseController {

    private final IDnnyunStoreService dnnyunStoreService;
    private final IDnnyunDataService dnnyunDataService;

    /**
     * 查询东云区域门店列表
     */
    @SaCheckPermission("data:dnnyun:store:list")
    @GetMapping("/list")
    public R<List<DnnyunStoreVo>> list(DnnyunStoreBo bo) {
        List<DnnyunStoreVo> list = dnnyunStoreService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询东云区域门店列表
     */
    @GetMapping("/treeList")
    public R<List<Tree<Long>>> treeList(DnnyunStoreBo bo) {
        List<DnnyunStoreVo> list = dnnyunStoreService.queryList(bo);
        List<Tree<Long>> treeList = dnnyunStoreService.buildTreeSelect(list);
        return R.ok(treeList);
    }

    /**
     * 刷新祖级
     */
    @GetMapping("/refreshAncestors")
    public R refreshAncestors(Long storeId) {
        dnnyunStoreService.refreshAncestors(storeId);
        return R.ok();
    }

    /**
     * 导出东云区域门店列表
     */
    @SaCheckPermission("data:dnnyun:store:export")
    @Log(title = "东云区域门店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DnnyunStoreBo bo, HttpServletResponse response) {
        List<DnnyunStoreVo> list = dnnyunStoreService.queryList(bo);
        ExcelUtil.exportExcel(list, "东云区域门店", DnnyunStoreVo.class, response);
    }

    /**
     * 获取东云区域门店详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("data:dnnyun:store:query")
    @GetMapping("/{id}")
    public R<DnnyunStoreVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(dnnyunStoreService.queryById(id));
    }

    /**
     * 新增东云区域门店
     */
    @SaCheckPermission("data:dnnyun:store:add")
    @Log(title = "东云区域门店", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DnnyunStoreBo bo) {
        return toAjax(dnnyunStoreService.insertByBo(bo));
    }

    /**
     * 修改东云区域门店
     */
    @SaCheckPermission("data:dnnyun:store:edit")
    @Log(title = "东云区域门店", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DnnyunStoreBo bo) {
        return toAjax(dnnyunStoreService.updateByBo(bo));
    }

    /**
     * 删除东云区域门店
     *
     * @param ids 主键串

    @SaCheckPermission("data:dnnyun:store:remove")
    @Log(title = "东云区域门店", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(dnnyunStoreService.deleteWithValidByIds(List.of(ids), true));
    }
*/
    /**
     * 同步东云区域门店
     */
    @SaCheckPermission("data:dnnyun:store:sync")
    @GetMapping("/sync")
    public R<Void> sync() {
        dnnyunDataService.syncStore();
        return R.ok();
    }
}
